<?php
/**
 * 图像处理
 */

header('Content-type:image/jpeg');

// // 创建画布
// $resource = imageCreateTrueColor(500, 500);
//
// // 设置颜色
// $red = imageColorAllocate($resource, 255, 0, 0); // RGB 255,0,0 红色
// $green = imageColorAllocate($resource, 0, 255, 0); // RGB 0,255,0 蓝色
// // 填充颜色
// imagefill($resource, 0, 0, $red);
// // 绘制空心矩形
// imagerectangle($resource, 100, 100, 200, 200, $green);
//
// // 绘制线条
// imageline($resource, 0, 0, 499, 499, $green);
//
// // 绘制像素
// for ($i = 0; $i < 600; $i++) {
//     imagesetpixel($resource, mt_rand(0, 500), mt_rand(0, 500), $green);
// }
//
// // 输入文本
// $font = realpath('D:\www\dscmall-x\storage\app\public\data\attached\fonts\msyh.ttf'); // 字体文件路径
// imagettftext($resource, 50, 0, 0, 50, $green, $font, 'test.com');
//
// // 输出图片
// imagejpeg($resource);
//
//
// // 释放图像
// imageDestroy($resource);

/**
 * 验证码实现
 */
class Captcha
{
    protected $width;
    protected $height;
    protected $size;
    protected $font;

    protected $res;
    protected $len = 4;

    public function __construct(int $width = 100, int $height = 30, $size = 20)
    {
        $this->width = $width;
        $this->height = $height;
        $this->size = $size;

        $this->font = realpath('D:\www\dscmall-x\storage\app\public\data\attached\fonts\msyh.ttf');
    }

    public function make()
    {
        $res = imagecreatetruecolor($this->width, $this->height);
        imagefill($this->res = $res, 0, 0, imagecolorallocate($res, 200, 200, 200));
        $this->text();
        $this->line();
        $this->pix();
        $this->render();
    }

    protected function text()
    {
        $text = 'abcdefghijklmnopqrstuvwxyz123456789';

        for ($i = 0; $i < $this->len; $i++) {
            $x = $this->width / $this->len * $i;
            $box = imagettfbbox($this->size, 0, $this->font, $text[mt_rand(0, strlen($text) - 1)]);
            imagettftext(
                $this->res,
                $this->size,
                mt_rand(-20, 20),
                $x,
                $this->height / 2 + ($box[0] - $box[7]) / 2,
                $this->textColor(),
                $this->font,
                strtoupper($text[mt_rand(0, strlen($text) - 1)])
            );
        }
    }

    protected function pix()
    {
        for ($i = 0; $i < 300; $i++) {
            imagesetpixel(
                $this->res,
                mt_rand(0, $this->width),
                mt_rand(0, $this->height),
                $this->color()
            );
        }
    }

    protected function line()
    {
        for ($i = 0; $i < 6; $i++) {
            imagesetthickness($this->res, mt_rand(1, 3));
            imageline(
                $this->res,
                mt_rand(0, $this->width),
                mt_rand(0, $this->height),
                mt_rand(0, $this->width),
                mt_rand(0, $this->height),
                $this->color()
            );
        }
    }

    protected function color()
    {
        return imagecolorallocate($this->res, mt_rand(100, 200), mt_rand(100, 200), mt_rand(100, 200));
    }

    protected function textColor()
    {
        return imagecolorallocate($this->res, mt_rand(50, 150), mt_rand(50, 150), mt_rand(50, 150));
    }

    protected function render()
    {
        header('Content-type:image/png');
        imagepng($this->res);
    }

}

// 后台code.php

// $code = new Captcha(100, 30, 20);
// $code->make();

// 前台
// <img src="code.php" alt="">


class image
{
    protected $image;
    protected $info;

    protected $resource;

    public function __construct($image = '')
    {
        $this->image = $image;

        $this->info = getimagesize($image);

        $this->resource = $this->resource($image);
    }

    // 生成水印
    public function water(string $water_img, string $filename = null, int $pos = 9)
    {
        $water = $this->resource($water_img);

        $position = $this->position($this->resource, $water, $pos);

        imagecopy($this->resource, $water, $position['x'], $position['y'], 0, 0, imagesx($water), imagesy($water));

        return $this->imageinfo();
    }

    // 生成缩略图
    public function thumb(int $width = 200, int $height = 200, int $type = 3)
    {
        $image = $this->resource;

        $info = $this->size($width, $height, imagesx($image), imagesy($image), $type);
        $res = imagecreatetruecolor($info[0], $info[1]);
        imagecopyresampled($res, $image, 0, 0, 0, 0, $info[0], $info[1], $info[2], $info[3]);

        header('Content-type:image/jpeg');
        return imagejpeg($res);
    }

    //获取资源对象
    protected function resource($image)
    {
        $info = getimagesize($image);
        $function = [1 => 'imagecreatefromgif', 2 => 'imagecreatefromjpeg', 3 => 'imagecreatefrompng'];
        $call = $function[$info[2]];
        return $call($image);
    }

    //根据类型输出图片
    protected function imageinfo()
    {
        $function = [1 => 'imagegif', 2 => 'imagejpeg', 3 => 'imagepng'];
        return $function[$this->info[2]];
    }

    //位置
    protected function position($des, $res, int $pos)
    {
        $info = ['x' => 0, 'y' => 0];
        switch ($pos) {
            case 1:
                break;
            case 2:
                $info['x'] = (imagesx($des) - imagesx($res)) / 2;
                break;
            case 3:
                $info['x'] = (imagesx($des) - imagesx($res));
                break;
            case 4:
                $info['y'] = (imagesy($des) - imagesy($res)) / 2;
                break;
            case 5:
                $info['x'] = (imagesx($des) - imagesx($res)) / 2;
                $info['y'] = (imagesy($des) - imagesy($res)) / 2;
                break;
        }
        return $info;
    }

    protected function size($rw, $rh, $iw, $ih, int $type)
    {
        switch ($type) {
            case 1:
                //固定宽度，高度自动
                $rh = $rw / $iw * $ih;
                break;
            case 2:
                //高度固定，宽度自动
                $rw = $rh / $ih * $iw;
                break;
            case 3:
                //固定宽度，高度裁切
                $ih = $iw / $rw * $rh;
                break;
            default:
                if (($iw / $rw) > ($ih / $rh)) {
                    $iw = $ih / $rh * $rh;
                } else {
                    $ih = $iw / $rw * $rw;
                }
        }

        return [$rw, $rh, $iw, $ih];
    }

}


$img = 'D:\www\dscmall-x\storage\app\public\data\attached\avatar\user_default1.jpg';
$image = new image($img);

// $water = 'D:\www\dscmall-x\storage\app\public\data\attached\avatar\default.png';
// $w = $image->water($water);
//
// echo $w;

$th = $image->thumb();


